#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=500000;

int solve(int k, int l1, int r1, int *a, int l2, int r2, int *b) {
  for (int i=l1, j=l2; i<=r1 || j<=r2; ) {
    k--;
    int now;
    if (j>r2 || (j<=r2 && i<=r1 && a[i]<=b[j])) {
      now=a[i];
      i++;
    } else {
      now = b[j];
      j++;
    }
    if (!k) {
      return now;
    }
  }
  return -1;
}

int main() {
  freopen("median.in", "r", stdin);
  freopen("median_bf.out", "w", stdout);

  static int a[maxn+1], b[maxn+1];
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=n; i++) {
    scanf("%d", a+i);
  }
  for (int i=1; i<=n; i++) {
    scanf("%d", b+i);
  }

  for (int i=1; i<=m; i++) {
    int opt;
    scanf("%d", &opt);
    if (opt==1) {
      int x, y, z;
      scanf("%d %d %d", &x, &y, &z);
      if (x) {
	b[y] = z;
      } else {
	a[y] = z;
      }
    } else {
      int l1, r1, l2, r2;
      scanf("%d %d %d %d", &l1, &r1, &l2, &r2);
      printf("%d\n", solve((r1-l1+r2-l2+2)/2+1, l1, r1, a, l2, r2, b));
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
